package ru.st.selenium.applogic2;

import static org.junit.Assert.assertTrue;
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import ru.st.selenium.applogic.FilmHelper;
import ru.st.selenium.model.Film;

public class FilmHelper2 extends DriverBasedHelper implements FilmHelper {

	public FilmHelper2(ApplicationManager2 manager) {
		super(manager.getWebDriver());
	}

	@Override
	public void create(Film film) {
		// TODO Auto-generated method stub
	}

	@Override
	public void createMovieOK() {
		pages.addMoviePage.setTitleValue("New Movie");
		pages.addMoviePage.setAkaValue("Film for Test");
		pages.addMoviePage.setAudioValue("Audio");
		pages.addMoviePage.setCastValue("Cast");
		pages.addMoviePage.setCountryValue("Country");
		pages.addMoviePage.setDirectorValue("Director");
		pages.addMoviePage.setDurationValue("120");
		pages.addMoviePage.setGenresValue("Generes");
		pages.addMoviePage.setMusicValue("Music");
		pages.addMoviePage.setNotesValue("Notes");
		pages.addMoviePage.setRatingValue("10.0");
		pages.addMoviePage.setSubtitlesValue("Subtitles");
		pages.addMoviePage.selectSeen_noRadioButton();
		pages.addMoviePage.setYearValue("2014");
		pages.addMoviePage.setFormatValue("DVD");
		pages.addMoviePage
				.selectCover("C:\\Users\\alexander.pesin\\Pictures\\New folder\\1219982812_28.jpg");
		pages.addMoviePage.clickSaveButton();
		pages.viewMoviePage.ensurePageLoaded();
	}

	@Override
	public void deleteFilm(String filmName) {
		pages.userHomePage.ensurePageLoaded().setSearchValue(filmName);
		pages.userHomePage.SelectFilm(filmName);
		pages.viewMoviePage.clickRemoveIcon();
		wait.until(alertIsPresent()).accept();
	}

	@Override
	public List<Film> search(String title) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void falseTitle() {
		pages.addMoviePage.clearTitleValue();
		pages.addMoviePage.setYearValue("2013");
		pages.addMoviePage.setDurationValue("90");
		pages.addMoviePage.clickSaveIcon();
		assertTrue(isElementPresent(By.id("imdbsearch")));
		// assertFalse(isElementPresent(By.xpath("//form[@id='updateform']/table/tbody/tr[4]/td[2]/label")));
		// pages.addMoviePage.ensurePageLoaded();
	}

	@Override
	public void falseYear() {
		pages.addMoviePage.setTitleValue("False Year");
		pages.addMoviePage.setDurationValue("90");
		pages.addMoviePage.clearYearValue();
		pages.addMoviePage.clickSaveIcon();
		assertTrue(isElementPresent(By.id("imdbsearch")));

	}

	@Override
	public void falseFormat() {
		pages.addMoviePage.clearFormatField();
		pages.addMoviePage.setTitleValue("False Format Test");
		pages.addMoviePage.setYearValue("2014");
		pages.addMoviePage.setDurationValue("90");
		pages.addMoviePage.clickSaveIcon();
		assertTrue(isElementPresent(By.id("imdbsearch")));
		// assertTrue(isElementPresent(By.xpath("//form[@id='updateform']/table/tbody/tr[7]/td[2]/label")));
		// pages.addMoviePage.ensurePageLoaded();
	}

	@Override
	public void yearIsNotNumber() {
		pages.addMoviePage.setTitleValue("Year is not number");
		pages.addMoviePage.setDurationValue("90");
		pages.addMoviePage.setYearValue("ABC");
		pages.addMoviePage.setFormatValue("DVD");
		pages.addMoviePage.clickSaveIcon();
		assertTrue(isElementPresent(By
				.xpath("//form[@id='updateform']/table/tbody/tr[4]/td[2]/label")));
		assertTrue(isElementPresent(By.id("imdbsearch")));
		// pages.addMoviePage.ensurePageLoaded();
	}

	@Override
	public void verifyErrorLabelExistance() {
		assertTrue(isElementPresent(By
				.xpath("//form[@id='updateform']/table/tbody/tr[2]/td[2]/label")));
		assertTrue(isElementPresent(By
				.xpath("//form[@id='updateform']/table/tbody/tr[4]/td[2]/label")));
	}

	private List<WebElement> getErrorLabels() {
		return driver
				.findElements(By
						.xpath("//form[@id='updateform']/table/tbody/tr[2]/td[2]/label"));
	}

}
